<template>
	<view class="container">
		<uni-forms ref="baseForm" :rules="rules" :modelValue="baseFormData" label-width="80px">
			<uni-forms-item label="腕带号" class="wounded" name="bh" required>
				<uni-easyinput :disabled="disabled" class="wounded-input" v-model="baseFormData.bh"
					placeholder="请输入腕带号" />
				<!-- <button class="wounded-button">腕带扫描</button>style="width: 150px;float: left;" -->

			</uni-forms-item>
			<uni-forms-item label="姓名" class="wounded" required name="xm">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.xm" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="性别" class="wounded" required name="xb">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.xb" :localdata="sex" />
			</uni-forms-item>
			<uni-forms-item label="年龄"  required class="wounded" name="nl">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.nl" placeholder="请输入年龄" />
			</uni-forms-item>
			<uni-forms-item label="负伤时间" class="wounded" required name="fssj">
				<uni-datetime-picker :disabled="disabled" type="datetime" v-model="baseFormData.fssj" />
			</uni-forms-item>
			<uni-forms-item label="到达时间" class="wounded" required name="ddsj">
				<uni-datetime-picker :disabled="disabled" type="datetime" v-model="baseFormData.ddsj" />
			</uni-forms-item>
			<uni-forms-item label="负伤地点" class="wounded" required name="fsdd">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.fsdd" placeholder="请输入负伤地点" />
			</uni-forms-item>
			<uni-forms-item label="部别" class="wounded">
				<uni-easyinput :disabled="disabled" v-model="baseFormData.bb" placeholder="请输入部别" />
			</uni-forms-item>
			<uni-forms-item label="职务" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.zw" :localdata="typeArrOffice"
					placeholder="请选择职务" />
			</uni-forms-item>
			<uni-forms-item label="军衔" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.jx" :localdata="typeArrMilitary"
					placeholder="请选择军衔" />
			</uni-forms-item>
			<uni-forms-item label="血型" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.xx" :localdata="typeArrBlood"
					placeholder="请选择血型" />
			</uni-forms-item>
			<uni-forms-item label="民族" class="wounded">
				<uni-data-select :disabled="disabled" v-model="baseFormData.mz" :localdata="typeArrEthnic"
					placeholder="请输入民族" />
			</uni-forms-item>
			<uni-forms-item label="家庭住址" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.jtzz" placeholder="请输入家庭住址" />
			</uni-forms-item>
			<uni-forms-item label="伤部" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.sb" multiple :localdata="hobbysWounded" />
			</uni-forms-item>
			<uni-forms-item label="伤类" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.sl" multiple
					:localdata="hobbysInjuries" />
			</uni-forms-item>
			<uni-forms-item label="伤型" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.sx" multiple
					:localdata="hobbysInjuryType" />
			</uni-forms-item>
			<uni-forms-item label="并发症" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.bfz" multiple
					:localdata="hobbysComplication" />
			</uni-forms-item>
			<uni-forms-item label="伤势" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.ss" multiple
					:localdata="hobbysInjuryForce" />
			</uni-forms-item>
			<uni-forms-item label="伤票类型" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.splx" multiple
					:localdata="hobbysTicketType" />
			</uni-forms-item>
			<uni-forms-item label="负伤类型" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.fslx" multiple
					:localdata="hobbysTypeInjury" />
			</uni-forms-item>
			<uni-forms-item label="处置方式" class="wounded">
				<uni-data-checkbox :disabled="disabled" v-model="baseFormData.czfs" multiple
					:localdata="hobbysDisposal" />
			</uni-forms-item>
			<uni-forms-item label="主诉" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.zs" placeholder="请输入主诉" />
			</uni-forms-item>
			<uni-forms-item label="现病史" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.xbs" placeholder="请输入现病史" />
			</uni-forms-item>
			<uni-forms-item label="摘要" class="wounded">
				<uni-easyinput :disabled="disabled" type="textarea" v-model="baseFormData.bz" placeholder="请输入摘要" />
			</uni-forms-item>
			<view class="fixedBottom">
				<button v-if="!disabled" class="woundedSave" @click="woundedSave('baseForm')">提交</button>
				<button v-if="!disabled" class="woundedList" @click="woundedList()">伤员列表</button>
			</view>
		</uni-forms>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import {
		addHuanzhe,
		posts,
		military,
		bloodType,
		Injuries,
		InjuryType,
		ethnicGroup,
		complication,
		Wounds,
		Woundst,
		InjuryTicketType,
		TypeOfInjury,
		Disposal,
		viewWounded,
		WoundedById,
		update
	} from '../../api/huanzhe.js'
	import {
		log,
		getTagIndex

	} from "../../common/util.js";
	import {
		toast,
		setStorageSync
	} from "../../utils/utils.js"
	// 获取 module
	// var rfidModule = uni.requireNativePlugin("DeviceModule_RFID");
	export default {
		data() {
			return {
				disabled: false,
				//扫描页面
				scanCbFilter: false,
				scanFilterAddress: "32",
				scanFilterLength: "96",
				scanFilterData: "",
				scanFilterBank: "1",
				buttonText: "开始扫描",
				tagCount: 0,
				count: 0,
				tagList: [],
				pageSize: 10,
				pageCurrent: 1,
				tableList: [],



				rules: {
					bh: {
						rules: [{
							required: true,
							errorMessage: '请扫描腕带号'
						}],
					},
					xm: {
						rules: [{
							required: true,
							errorMessage: '请 输入名称'
						}],
					},
					xb: {
						rules: [{
							required: true,
							errorMessage: '请选择性别名称'
						}],
					},
					fssj: {
						rules: [{
							required: true,
							errorMessage: '请输入负伤时间'
						}],
					},
					nl:{
						rules: [{
							required: true,
							errorMessage: '请输入年龄'
						}],
					},
					fsdd: {
						rules: [{
							required: true,
							errorMessage: '请输入负伤地点'
						}],
					}
				},
				baseFormData: {
					bh: null,
					xm: null,
					xb: null,
					nl: null,
					fssj: null,
					ddsj: null,
					fsdd: null,
					bb: null,
					zw: '上士',
					jx: null,
					xx: null,
					mz: null,
					jtzz: null,
					sb: [],
					sl: [],
					sx: [],
					bfz: [],
					ss: [],
					splx: [],
					fslx: [],
					czfs: [],
					zs: null,
					xbs: null,
					bz: null
				},
				hobbysDisposal: [],
				hobbysTypeInjury: [],
				hobbysTicketType: [],
				hobbysInjuryForce: [],
				hobbysComplication: [],
				hobbysInjuryType: [],
				hobbysInjuries: [],
				hobbysWounded: [],
				typeArrOffice: [],
				typeArrMilitary: [],
				typeArrBlood: [],
				typeArrEthnic: [],
				sex: [{
						text: '男',
						value: '男'
					},
					{
						text: '女',
						value: '女'
					}
				],
				ss: {
					value: undefined,
					text: undefined
				},
				judgment: null,
				id: null
			}
		},
		onLoad(option) {
			this.postss()
			this.getWoundedById(option)
			this.judgment = option.option
			this.id = option.bh
			let self = this
			// #ifdef APP-PLUS
			// 手柄按键监听事件，可在此编写一些逻辑，如使用按键触发扫描，
			plus.key.addEventListener('keydown', function(KeyEvent) {
				log("按下了键：" + KeyEvent.keyCode);
				if (KeyEvent.keyCode == 293) {
					self.startScan()
				}
			});
			// #endif   
			switch (option.option) {
				case '1': {
					uni.setNavigationBarTitle({
						title: '修改伤员信息'
					})
					break;
				}
				case '2': {
					uni.setNavigationBarTitle({
						title: '伤员信息详情'
					})
					break;
				}
			}
		},
		mounted() {

			setTimeout(() => {
				// 使用模块前必须先初始化RDIF模块
				let res = rfidModule.init();
				log(JSON.stringify(res));
				if (res.code == 0) {
					this.isLoading = false;
				} else if (res.code == -1) {
					this.loadingText = 'RFID模块加载失败'
					// uni.showToast({
					//     title: 'RFID模块加载失败'
					// })
				}
			}, 400);

		},
		beforeDestroy() {
			// 使用完毕必须释放RDIF模块
			let res = rfidModule.free();
			log("free: " + JSON.stringify(res))
		},
		methods: {
			getWoundedById(option) {
				log(option)
				if (option.bh != null) {
					WoundedById(option.bh).then(res => {

						for (let key in res.data) {


							const arr = ['sb', 'sl', 'sx', 'bfz', 'ss', 'splx', 'fslx', 'czfs']
							this.baseFormData[key] = res.data[key]
							arr.forEach((item, index) => {
								if (key === item) {

									if (RegExp(/,/).test(res.data[key])) {

										this.baseFormData[item] = res.data[item].split(',')
									} else {
										this.baseFormData[item] = [res.data[item]]

									}
								}
							})
						}
						if (option.option == 2) {
							this.disabled = true
						}
					})
				}
			},
			async startScan() {
				log('rfidModule = ' + rfidModule)
				// 调用异步方法
				rfidModule.startScan(
					(res) => {
						log(JSON.stringify(res))
						if (res.code == 0) {
							uni.showToast({
								title: '开启扫描成功',
							})
						} else if (res.code == 1) {
							rfidModule.stopScan()
							// this.lists.unshift(JSON.stringify(data))
							if (res.data != null && res.data.length > 0) {
								for (let i = 0; i < res.data.length; i++) {
									let tag = res.data[i];
									let tagExists = [];
									//过滤重复标签
									let index = getTagIndex(this.tagList, tag.epc, tagExists)
									if (tagExists[0]) { //标签已存在
										this.tagList[index].rssi = tag.rssi;
										this.tagList[index].ant = tag.antennaPort;
										this.tagList[index].count++;
									} else { //标签不存在
										this.tagList.splice(index, 0, {
											epc: tag.epc,
											tid: tag.tid || '',
											user: tag.user || '',
											rssi: tag.rssi,
											pc: tag.pc,
											count: 1
										});
										this.tagCount = this.tagList.length
									}
									this.count++;
								}

								this.pageCurrent = 1
								this.tableList = this.tagList.slice(
									(this.pageCurrent - 1) * this.pageSize,
									this.pageCurrent * this.pageSize
								);
								this.baseFormData.wristbands = this.tableList[0].epc
							}
						}
					}
				);
			},
			stopScan() {
				// 调用同步方法
				var res = rfidModule.stopScan()
				log(JSON.stringify(res));
				if (res.code == 0) {
					uni.showToast({
						title: '关闭扫描成功',
					})
				} else {
					uni.showToast({
						icon: "error",
						title: res.message,
					})
				}
			},
			postss() {
				// 职务
				posts().then(res => {
					res.data.forEach((item, index) => {
						var ArrOffice = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.typeArrOffice.push(ArrOffice)
					})
					setStorageSync('typeArrOffice', this.typeArrOffice)
				})
				// 军衔
				military().then(res => {
					res.data.forEach((item, index) => {
						var ArrMilitary = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.typeArrMilitary.push(ArrMilitary)
					})
					setStorageSync('typeArrMilitary', this.typeArrMilitary)
				})
				// 血型
				bloodType().then(res => {
					res.data.forEach((item, index) => {
						var ArrBlood = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.typeArrBlood.push(ArrBlood)
					})
					setStorageSync('typeArrBlood', this.typeArrBlood)
				})
				// 伤类
				Injuries().then(res => {
					res.data.forEach((item, index) => {
						var hobbysInjuries = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysInjuries.push(hobbysInjuries)
					})
					setStorageSync('hobbysInjuries', this.hobbysInjuries)
				})
				// 伤型
				InjuryType().then(res => {
					res.data.forEach((item, index) => {
						var hobbysInjuryType = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysInjuryType.push(hobbysInjuryType)
					})
					setStorageSync('hobbysInjuryType', this.hobbysInjuryType)
				})
				// 民族
				ethnicGroup().then(res => {
					res.data.forEach((item, index) => {
						var typeArrEthnic = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.typeArrEthnic.push(typeArrEthnic)
					})
					setStorageSync('typeArrEthnic', this.typeArrEthnic)
				})
				// 并发症
				complication().then(res => {
					res.data.forEach((item, index) => {
						var hobbysComplication = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysComplication.push(hobbysComplication)
					})
					setStorageSync('hobbysComplication', this.hobbysComplication)
				})
				// 伤部
				Wounds().then(res => {
					res.data.forEach((item, index) => {
						var hobbysWounded = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysWounded.push(hobbysWounded)
					})
					setStorageSync('hobbysWounded', this.hobbysWounded)
				})
				// 伤势
				Woundst().then(res => {
					res.data.forEach((item, index) => {
						var hobbysInjuryForce = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysInjuryForce.push(hobbysInjuryForce)
					})
					setStorageSync('hobbysInjuryForce', this.hobbysInjuryForce)
				})
				// 伤票类型
				InjuryTicketType().then(res => {
					res.data.forEach((item, index) => {
						var hobbysTicketType = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysTicketType.push(hobbysTicketType)
					})
					setStorageSync('hobbysTicketType', this.hobbysTicketType)
				})
				// 负伤类型
				TypeOfInjury().then(res => {
					res.data.forEach((item, index) => {
						var hobbysTypeInjury = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysTypeInjury.push(hobbysTypeInjury)
					})
					setStorageSync('hobbysTypeInjury', this.hobbysTypeInjury)
				})
				// 处置方式
				Disposal().then(res => {
					res.data.forEach((item, index) => {
						var hobbysDisposal = {
							value: item.dictValue,
							text: item.dictLabel
						}
						this.hobbysDisposal.push(hobbysDisposal)
					})
					setStorageSync('hobbysDisposal', this.hobbysDisposal)
				})
			},
			// 保存
			woundedSave(ref) {
				this.$refs[ref].validate().then(res => {
					console.log(this.judgment == 1);
					if (this.judgment == 1) {
						var baseData = {
							// id
							id: this.id,
							// 编号
							bh: this.baseFormData.bh,
							// 姓名
							xm: this.baseFormData.xm,
							// 性别
							xb: this.baseFormData.xb,
							// 年龄
							nl: this.baseFormData.nl,
							// 负伤时间
							fssj: this.baseFormData.fssj,
							// 到达时间
							ddsj: this.baseFormData.ddsj,
							// 负伤地点
							fsdd: this.baseFormData.fsdd,
							// 部别
							bb: this.baseFormData.bb,
							// 职务
							zw: this.baseFormData.zw,
							// 军衔
							jx: this.baseFormData.jx,
							// 血型
							xx: this.baseFormData.xx,
							// 民族
							mz: this.baseFormData.mz,
							// 家庭住址
							jtzz: this.baseFormData.jtzz,
							// 伤部
							sb: this.baseFormData.sb.join(','),
							// 伤类
							sl: this.baseFormData.sl.join(','),
							// 伤型
							sx: this.baseFormData.sx.join(','),
							// 并发症
							bfz: this.baseFormData.bfz.join(','),
							// 伤势
							ss: this.baseFormData.ss.join(','),
							//伤票类型
							splx: this.baseFormData.splx.join(','),
							// 负伤类型
							fslx: this.baseFormData.fslx.join(','),
							// 处置方式
							czfs: this.baseFormData.czfs.join(','),
							// 主诉
							zs: this.baseFormData.zs,
							// 现病史
							xbs: this.baseFormData.xbs,
							// 摘要
							bz: this.baseFormData.bz
						}
						update(baseData).then(res => {
							toast(res.msg)
							this.baseFormData = {}
							if (res.code == 200) {
								uni.redirectTo({
									url: '/pages/casualtyList/index'
								})
							}
						})
					} else {
						var baseData = {
							// 编号
							bh: this.baseFormData.bh,
							// 姓名
							xm: this.baseFormData.xm,
							// 性别
							xb: this.baseFormData.xb,
							// 年龄
							nl: this.baseFormData.nl,
							// 负伤时间
							fssj: this.baseFormData.fssj,
							// 到达时间
							ddsj: this.baseFormData.ddsj,
							// 负伤地点
							fsdd: this.baseFormData.fsdd,
							// 部别
							bb: this.baseFormData.bb,
							// 职务
							zw: this.baseFormData.zw,
							// 军衔
							jx: this.baseFormData.jx,
							// 血型
							xx: this.baseFormData.xx,
							// 民族
							mz: this.baseFormData.mz,
							// 家庭住址
							jtzz: this.baseFormData.jtzz,
							// 伤部
							sb: this.baseFormData.sb.join(','),
							// 伤类
							sl: this.baseFormData.sl.join(','),
							// 伤型
							sx: this.baseFormData.sx.join(','),
							// 并发症
							bfz: this.baseFormData.bfz.join(','),
							// 伤势
							ss: this.baseFormData.ss.join(','),
							//伤票类型
							splx: this.baseFormData.splx.join(','),
							// 负伤类型
							fslx: this.baseFormData.fslx.join(','),
							// 处置方式
							czfs: this.baseFormData.czfs.join(','),
							// 主诉
							zs: this.baseFormData.zs,
							// 现病史
							xbs: this.baseFormData.xbs,
							// 摘要
							bz: this.baseFormData.bz
						}
						addHuanzhe(baseData).then(res => {
							toast(res.msg)
							this.baseFormData = {}
							if (res.code == 200) {
								uni.redirectTo({
									url: '/pages/casualtyList/index'
								})
							}
						})
					}
				}).catch(err => {
					console.log('err', err);
					this.baseFormData = {}
				})
			},
			// 伤员列表
			woundedList() {
				uni.redirectTo({
					url: '/pages/casualtyList/index'
				})
			}
		},
	}
</script>

<style lang="scss">

	
	
	::v-deep.uni-section .uni-section-header__decoration {
		background-color: #23440d !important;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .radio__inner {
		border-color: #23440d !important;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .radio__inner .radio__inner-icon {
		background-color: #23440d !important;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .checklist-text {
		color: #23440d;
	}

	::v-deep.uni-data-checklist .checklist-group .checklist-box.is--default.is-checked .checkbox__inner {
		border-color: #23440d;
		background-color: #23440d;
	}

	::v-deep.is-input-border,::v-deep.uni-date-x--border,::v-deep.uni-select {
		border: none !important;
	}

	.container {
		padding-bottom: 200rpx;
	}

	/*腕带号输入框*/
	// .wounded-input{
	// 	width: 40%;
	// 	float: left;
	// }
	/*输入框样式*/
	.wounded {
		// margin: 5%;
		padding: 5% 5% 0;

		display: flex;
		align-items: center;
		justify-content: space-between;
		box-shadow: 1px 2px 3px #efefef inset !important;

	}

	/*腕带扫描样式*/
	.wounded-button {
		width: 100px;
		height: 70rpx;
		border-radius: 15px;
		font-size: 28rpx;
		line-height: 70rpx;
		color: #FFF;
		outline: none;
		background-color: #23440d;
		letter-spacing: 1px;
	}

	/*表单样式*/
	.wounded-name {
		width: 80%;
	}

	.fixedBottom {
		height: 200rpx;
		background-color: #FFF;
		position: fixed;
		bottom: 0rpx;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 50;

		button {
			width: 300rpx;
			height: 88rpx;
			color: white;
			background-color: #064e3b;
			border-radius: 20px;
			line-height: 88rpx;
			text-align: center;
			border-radius: 88rpx;
		}
	}
</style>